#ifndef SEMOPHORE_H
#define SEMOPHORE_H

#include <mutex>
#include <condition_variable>
#include <memory>

using namespace std;

class Semophore {
public:
	Semophore(int res=0):res_(res){}
	~Semophore() = default;

	void wait()
	{
		unique_lock<mutex> lock(mtx_);
		while (res_ <= 0) {
			cond_.wait(lock);
		}
		res_--;
	}
	void post()
	{
		unique_lock<mutex> lock(mtx_);
		res_++;
		cond_.notify_all();
	}
private:
	mutex mtx_;
	condition_variable cond_;
	int res_;
};
#endif